
import handleNum from './index';

let reducer = (state = { ...handleNum.state }, action: { type: string, val: number }) => {

  // 调用dispatch后执行这里的代码
  let newState = JSON.parse(JSON.stringify(state))
  // switch (action.type) {
  //   case handleNum.add1:
  //     handleNum.actions[handleNum.add1](newState, action)
  //     break;
  //   case handleNum.add2:
  //     handleNum.actions[handleNum.add2](newState, action)
  //     break;
  //   default:
  //     break;
  // }

  // 优化上面的 switch ... case写法
  // 利用action.type 和 actionNames 与每一项对比,如果相等,就调用
  // 模块名.actions[下标](newState, action)
  for (let key in handleNum.actionNames) {
    if (action.type === handleNum.actionNames[key]) {
      handleNum.actions[handleNum.actionNames[key]](newState, action);
      break;
    }
  }

  return newState
}

export default reducer;